<template>
  <el-card>
    <el-button
      @click="$router.push('/category/add')"
      icon="el-icon-circle-plus-outline"
      type="primary"
    >新增</el-button>
    <el-divider />
    <el-table
      :data="list"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="id" label="ID" min-width="80"></el-table-column>
      <el-table-column prop="catename" label="分类名称" min-width="180"></el-table-column>
      <el-table-column prop="img" label="分类图片" min-width="180">
        <template v-slot="scope">
          <el-image
            v-if="scope.row.img!=''"
            :src="$imgBaseUrl+scope.row.img"
            :style="{width:'100px'}"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" min-width="180">
        <template v-slot="scope">
          <el-tag type="success" v-if="scope.row.status==1">正常</el-tag>
          <el-tag type="error" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="180">
        <template v-slot="scope">
          <el-button
            @click="edit(scope.row.id)"
            type="primary"
            icon="el-icon-edit"
            circle
            size="mini"
          ></el-button>
          <el-button
            @click="del(scope.row.id)"
            type="danger"
            icon="el-icon-delete"
            circle
            size="mini"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script>
// 导入category.js
import { getCateList } from "../../request/category";
export default {
  data() {
    return {
      //  分类列表
      list: []
    };
  },
  methods: {
    edit(id) {
      this.$router.push(`/category/${id}`);
    },
    del(id) {
      this.$myConfirm(async () => {
        // 1-发送数据请求
        const res = await this.$http.post("/api/catedelete", { id });
        if (res.code == 200) {
          // 1-成功提示
          this.$success("删除成功");
          // 2-更新视图层
          const index = this.list.findIndex(item=>item.id == id);
          this.list.splice(index, 1);
        }
      });
    },
    // 获取分类列表
    async getCateList() {
      const list = await getCateList();
      if (list) {
        this.list = list;
      }
    }
  },
  created() {
    this.getCateList();
  }
};
</script>

<style>
</style>